{% extends './layout/default' %}

{% block breadcrumbs %}
    {{ Breadcrumbs.render(Route.getCurrentRoute.getName, transactionGroup) }}
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-lg-6">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'transaction_journal_information'|_ }}</h3>
                    <div class="btn-group btn-group-xs pull-right">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false">
                            {{ 'actions'|_ }} <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            {# edit + delete #}
                            <li><a href="{{ route('transactions.edit', [transactionGroup.id]) }}"><span
                                        class="fa fa-pencil"></span> {{ 'edit'|_ }}</a></li>
                            <li><a href="{{ route('transactions.delete', [transactionGroup.id]) }}"><span
                                        class="fa fa-trash"></span> {{ 'delete'|_ }}</a></li>
                            <li role="separator" class="divider"></li>

                            {# convert to different type #}
                            {% if groupArray.transactions[0].type != 'withdrawal' %}
                                <li>
                                    <a href="{{ route('transactions.convert.index', ['withdrawal', transactionGroup.id]) }}"><span
                                            class="fa fa-exchange"></span> {{ 'convert_to_withdrawal'|_ }}</a></li>
                            {% endif %}

                            {% if groupArray.transactions[0].type != 'deposit' %}
                                <li>
                                    <a href="{{ route('transactions.convert.index', ['deposit', transactionGroup.id]) }}"><span
                                            class="fa fa-exchange"></span> {{ 'convert_to_deposit'|_ }}</a></li>
                            {% endif %}

                            {% if groupArray.transactions[0].type != 'transfer' %}
                                <li>
                                    <a href="{{ route('transactions.convert.index', ['transfer', transactionGroup.id]) }}"><span
                                            class="fa fa-exchange"></span> {{ 'convert_to_transfer'|_ }}</a></li>
                            {% endif %}

                            {#  clone #}
                            {% if groupArray.transactions[0].type != 'opening balance' and groupArray.transactions[0].type != 'reconciliation' %}
                                <li role="separator" class="divider"></li>
                                <li><a href="#" class="clone-transaction" data-id="{{ transactionGroup.id }}"><span
                                            class="fa fa-copy"></span> {{ 'clone'|_ }}</a></li>
                                <li><a href="#" class="clone-transaction-and-edit" data-id="{{ transactionGroup.id }}"><span
                                            class="fa fa-copy"></span> {{ 'clone_and_edit'|_ }}</a></li>
                            {% endif %}

                        </ul>
                    </div>
                </div>
                <div class="box-body no-padding">
                    <table class="table table-hover">
                        <tbody>
                        <tr>
                            <td style="width:40%;">{{ trans('list.type') }}</td>
                            <td>{{ first.transactiontype.type|_ }}</td>
                        </tr>
                        <tr>
                            <td>{{ trans('list.description') }}</td>
                            <td>
                                {% if splits == 1 %}
                                    {{ first.description }}
                                {% else %}
                                    {{ transactionGroup.title }}
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <td style="width:30%;">{{ trans('list.date') }}</td>
                            <td>
                                <span class="date-time" data-date="{{ first.date.toIso8601ZuluString() }}" title="{{ first.date.isoFormat(dateTimeFormat) }}{% if(first.date_tz != '') %} ({{ trans('firefly.stored_in_tz', {timezone: first.date_tz }) }}, {{ trans('firefly.displayed_in_tz', {timezone: config('app.timezone') }) }}){% endif %}">
                                {{ first.date.isoFormat(dateTimeFormat) }}
                                {% if(first.date_tz != '') %}
                                    ({{ trans('firefly.stored_in_tz', {timezone: first.date_tz }) }})
                                {% endif %}
                                </span>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            {% if groupLogEntries|length > 0 %}
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            {{ 'audit_log_entries'|_ }}
                        </h3>
                    </div>
                    <div class="box-body no-padding">
                        {% include 'list.ale' with {logEntries: groupLogEntries} %}
                    </div>
                </div>
            {% endif %}

        </div>
        <div class="col-lg-6">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'transaction_journal_meta'|_ }}</h3>
                </div>
                <div class="box-body no-padding">
                    <table class="table table-hover">
                        <tbody>
                        {% if first.transactiontype.type != 'Withdrawal' or splits == 1 %}
                            <tr>
                                <td style="width:40%;">
                                    {{ trans_choice('firefly.source_accounts', accounts['source']|length ) }}
                                </td>
                                <td>
                                    {% for account in accounts['source'] %}
                                        {% if 'Cash account' == account.type %}
                                            <span class="text-success">({{ 'cash'|_ }})</span>
                                        {% else %}
                                            <a href="{{ route('accounts.show',account.id) }}"
                                               title="{{ account.iban|default(account.name) }}">
                                                {{ account.name }}
                                            </a>
                                        {% endif %}
                                        {% if loop.index0 != accounts['source']|length -1 %}, {% endif %}
                                    {% endfor %}
                                </td>
                            </tr>
                        {% endif %}

                        {% if first.transactiontype.type != 'Deposit' or splits == 1 %}
                            <tr>
                                <td>
                                    {{ trans_choice('firefly.destination_accounts', accounts['destination']|length ) }}

                                </td>
                                <td>
                                    {% for account in accounts['destination'] %}
                                        {% if 'Cash account' == account.type %}
                                            <span class="text-success">({{ 'cash'|_ }})</span>
                                        {% else %}
                                            <a href="{{ route('accounts.show',account.id) }}"
                                               title="{{ account.iban|default(account.name) }}">
                                                {{ account.name }}
                                            </a>
                                        {% endif %}
                                        {% if loop.index0 != accounts['destination']|length -1 %}, {% endif %}
                                    {% endfor %}
                                </td>
                            </tr>
                        {% endif %}
                        <tr>
                            <td style="width:30%;">{{ 'total_amount'|_ }}</td>
                            <td>
                                {% for amount in amounts %}
                                    {% if first.transactiontype.type == 'Withdrawal' %}
                                        {{ formatAmountBySymbol(amount.amount*-1,amount.symbol, amount.decimal_places) }}{% if loop.index0 != amounts|length -1 %}, {% endif %}
                                    {% elseif first.transactiontype.type == 'Deposit' %}
                                        {{ formatAmountBySymbol(amount.amount,amount.symbol, amount.decimal_places) }}{% if loop.index0 != amounts|length -1 %}, {% endif %}
                                    {% elseif first.transactiontype.type == 'Transfer' %}
                                        <span class="text-info money-transfer">
                                        {{ formatAmountBySymbol(amount.amount, amount.symbol, amount.decimal_places, false) }}{% if loop.index0 != amounts|length -1 %}, {% endif %}
                                    </span>
                                    {% elseif first.transactiontype.type == 'Opening balance' %}
                                        {# Opening balance stored amount is always negative: find out which way the money goes #}
                                        {% if groupArray.transactions[0].source_type == 'Initial balance account' %}
                                            {{ formatAmountBySymbol(amount.amount*-1,amount.symbol, amount.decimal_places) }}
                                        {% else %}
                                            {{ formatAmountBySymbol(amount.amount,amount.symbol, amount.decimal_places) }}
                                        {% endif %}
                                    {% endif %}
                                {% endfor %}
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row">

    </div>
    {% if splits > 1 %}
        <div class="row">
            <div class="col-lg-12">
                <h3>{{ 'splits'|_ }}</h3>
            </div>
        </div>
    {% endif %}
    {% set boxSize=6 %}
    {% if(splits == 2) %}
        {% set boxSize=6 %}
    {% endif %}
    {% if (splits > 2) %}
        {% set boxSize = 4 %}
    {% endif %}
    <div class="row">
        {% for index,journal in groupArray.transactions %}
            <div class="col-lg-{{ boxSize }}">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            {{ journal.description }}
                            {% if journal.reconciled %}
                                <span class="fa fa-check"></span>
                            {% endif %}
                            {% if splits > 1 %}
                                <small>
                                    {{ index+1 }} / {{ splits }}
                                </small>
                            {% endif %}
                        </h3>


                        <div class="btn-group btn-group-xs pull-right">
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                    aria-haspopup="true" aria-expanded="false">
                                {{ 'actions'|_ }} <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu">
                                {# edit + delete #}
                                <li><a href="{{ route('transactions.edit', [transactionGroup.id]) }}"><span
                                            class="fa fa-pencil"></span> {{ 'edit'|_ }}</a></li>
                                <li><a href="{{ route('transactions.delete', [transactionGroup.id]) }}"><span
                                            class="fa fa-trash"></span> {{ 'delete'|_ }}</a></li>
                                {% if journal.reconciled %}
                                <li><a class="reconcile-button" href="{{ route('transactions.unreconcile', [journal.transaction_journal_id]) }}"><span
                                            class="fa fa-history"></span> {{ 'unreconcile'|_ }}</a></li>
                                {% endif %}
                                <li role="separator" class="divider"></li>

                                {# convert to different type #}
                                {% if groupArray.transactions[0].type != 'withdrawal' %}
                                    <li>
                                        <a href="{{ route('transactions.convert.index', ['withdrawal', transactionGroup.id]) }}"><span
                                                class="fa fa-exchange"></span> {{ 'convert_to_withdrawal'|_ }}</a>
                                    </li>
                                {% endif %}

                                {% if groupArray.transactions[0].type != 'deposit' %}
                                    <li>
                                        <a href="{{ route('transactions.convert.index', ['deposit', transactionGroup.id]) }}"><span
                                                class="fa fa-exchange"></span> {{ 'convert_to_deposit'|_ }}</a></li>
                                {% endif %}

                                {% if groupArray.transactions[0].type != 'transfer' %}
                                    <li>
                                        <a href="{{ route('transactions.convert.index', ['transfer', transactionGroup.id]) }}"><span
                                                class="fa fa-exchange"></span> {{ 'convert_to_transfer'|_ }}</a>
                                    </li>
                                {% endif %}

                                {#  clone #}
                                {% if groupArray.transactions[0].type != 'opening balance' and groupArray.transactions[0].type != 'reconciliation' %}
                                    <li role="separator" class="divider"></li>
                                    <li><a href="#" data-id="{{ transactionGroup.id }}" class="clone-transaction"><span
                                                class="fa fa-copy"></span> {{ 'clone'|_ }}</a></li>
                                    <li><a href="#" data-id="{{ transactionGroup.id }}" class="clone-transaction-and-edit"><span
                                                class="fa fa-copy"></span> {{ 'clone_and_edit'|_ }}</a></li>
                                {% endif %}

                                <li><a href="#" class="link-modal"
                                       data-journal="{{ journal.transaction_journal_id }}"><span
                                            class="fa fa-fw fa-link"></span>{{ 'link_transaction'|_ }}</a></li>
                                <li role="separator" class="divider"></li>
                                <li>
                                    <a href="{{ route('rules.create-from-journal', [journal.transaction_journal_id]) }}"><span
                                            class="fa fa-fw fa-random"></span>{{ 'create_rule_from_transaction'|_ }}
                                    </a></li>
                                <li>
                                    <a href="{{ route('recurring.create-from-journal', [journal.transaction_journal_id]) }}"><span
                                            class="fa fa-fw fa-paint-brush"></span>{{ 'create_recurring_from_transaction'|_ }}
                                    </a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="box-body no-padding">
                        <table class="table">
                            <tr>
                                <td colspan="2">

                                    <!-- type is: "{{ first.transactiontype.type }}" -->
                                    {% if 'Cash account' == journal.source_type %}
                                        <span class="text-success">({{ 'cash'|_ }})</span>
                                    {% else %}
                                        <a href="{{ route('accounts.show', journal.source_id) }}"
                                           title="{{ journal.source_iban|default(journal.source_name) }}">{{ journal.source_name }}</a> &rarr;
                                    {% endif %}

                                    {% if first.transactiontype.type == 'Withdrawal' %}
                                        {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places) }}
                                    {% elseif first.transactiontype.type == 'Deposit' %}
                                        {{ formatAmountBySymbol(journal.amount, journal.currency_symbol, journal.currency_decimal_places) }}
                                    {% elseif first.transactiontype.type == 'Transfer' or first.transactiontype.type == 'Opening balance' %}
                                        <span class="text-info money-transfer">
                                        {{ formatAmountBySymbol(journal.amount, journal.currency_symbol, journal.currency_decimal_places, false) }}
                                    </span>
                                    {% elseif first.transactiontype.type == 'Liability credit' %}
                                        <span class="text-info money-transfer">
                                        {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}
                                    </span>
                                    {% endif %}

                                    <!-- do foreign amount -->
                                    {% if null != journal.foreign_amount %}
                                        {% if first.transactiontype.type == 'Withdrawal' %}
                                            ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places) }})
                                        {% elseif first.transactiontype.type == 'Deposit' %}
                                            ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places) }})
                                        {% elseif first.transactiontype.type == 'Transfer' %}
                                            <span class="text-info money-transfer">
                                        ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }})
                                    </span>
                                        {% endif %}
                                    {% endif %}

                                    &rarr;
                                    {% if 'Cash account' == journal.destination_type %}
                                        <span class="text-success">({{ 'cash'|_ }})</span>
                                    {% else %}
                                        <a href="{{ route('accounts.show', journal.destination_id) }}"
                                           title="{{ journal.destination_iban|default(journal.destination_name) }}">{{ journal.destination_name }}</a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% if null != journal.category_id %}
                                <tr>
                                    <td style="width:30%;">{{ 'category'|_ }}</td>
                                    <td>
                                        <a href="{{ route('categories.show', [journal.category_id]) }}">{{ journal.category_name }}</a>
                                    </td>
                                </tr>
                            {% endif %}
                            {% if null != journal.budget_id and first.transactiontype.type == 'Withdrawal' %}
                                <tr>
                                    <td style="width:40%;">{{ 'budget'|_ }}</td>
                                    <td>
                                        <a href="{{ route('budgets.show', [journal.budget_id]) }}">{{ journal.budget_name }}</a>
                                    </td>
                                </tr>
                            {% endif %}
                            {% if null != journal.bill_id and first.transactiontype.type == 'Withdrawal' %}
                                <tr>
                                    <td style="width:40%;">{{ 'bill'|_ }}</td>
                                    <td>
                                        <a href="{{ route('subscriptions.show', [journal.bill_id]) }}">{{ journal.bill_name }}</a>
                                    </td>
                                </tr>
                            {% endif %}
                            <!-- other fields -->
                            {% for dateField in ['interest_date','book_date','process_date','due_date','payment_date','invoice_date'] %}
                                {% if journalHasMeta(journal.transaction_journal_id, dateField) %}
                                    <tr>
                                        <td style="width:40%;">{{ trans('list.'~dateField) }}</td>
                                        <td>{{ journalGetMetaDate(journal.transaction_journal_id, dateField).isoFormat(monthAndDayFormat) }}</td>
                                    </tr>
                                {% endif %}
                            {% endfor %}
                            {% for metaField in ['external_id','bunq_payment_id','internal_reference','sepa_batch_id','sepa_ct_id','sepa_ct_op','sepa_db','sepa_country','sepa_cc','sepa_ep','sepa_ci','external_url'] %}
                                {% if journalHasMeta(journal.transaction_journal_id, metaField) %}
                                    <tr>
                                        <td>{{ trans('list.'~metaField) }}</td>
                                        <td style="width:40%;">
                                            {% if 'external_url' == metaField %}
                                                {% set url = journalGetMetaField(journal.transaction_journal_id, metaField) %}
                                                <a href="{{ url }}" rel="noopener noreferrer nofollow" target="_blank">
                                                    {% if url|length > 60 %}
                                                        {{ url|slice(0, 60) ~ '...' }}
                                                    {% else %}
                                                        {{ url }}
                                                    {% endif %}
                                                </a>
                                            {% endif %}
                                            {% if 'external_url' != metaField %}
                                                {{ journalGetMetaField(journal.transaction_journal_id, metaField) }}
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endif %}
                            {% endfor %}
                            {% if null != journal.notes and '' != journal.notes %}
                                <tr>
                                    <td style="width:40%;">{{ trans('list.notes') }}</td>
                                    <td class="markdown">{{ journal.notes|default('')|markdown }}</td>
                                </tr>
                            {% endif %}
                            {% if journalHasMeta(journal.transaction_journal_id, 'recurring_total') and journalHasMeta(journal.transaction_journal_id, 'recurring_count') %}
                                {% set recurringTotal = journalGetMetaField(journal.transaction_journal_id, 'recurring_total') %}
                                {% if 0 == recurringTotal %}
                                    {% set recurringTotal = '∞' %}
                                {% endif %}
                                <tr>
                                    <td style="width:40%;">{{ trans('list.recurring_transaction') }}</td>
                                    <td>{{ trans('firefly.recurring_info', {total: recurringTotal, count: journalGetMetaField(journal.transaction_journal_id, 'recurring_count') }) }}</td>
                                </tr>
                            {% endif %}
                            {% if journal.tags|length > 0 %}
                                <tr>
                                    <td style="width:40%;">{{ 'tags'|_ }}</td>
                                    <td>
                                        {% for tag in journal.tags %}
                                            <h4 style="display: inline;"><a class="label label-success"
                                                                            href="{{ route('tags.show', tag.id) }}">
                                                    <span class="fa fa-fw fa-tag"></span>{{ tag.tag }}</a>
                                            </h4>
                                        {% endfor %}
                                    </td>
                                </tr>
                            {% endif %}
                        </table>
                    </div>
                </div>

                <!-- Transaction links -->
                {% if links[journal.transaction_journal_id]|length > 0 %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">
                                {{ 'journal_links'|_ }}
                            </h3>
                        </div>
                        <div class="box-body no-padding">
                            <table class="table">
                                {% for link in links[journal.transaction_journal_id] %}
                                    <tr>
                                        <td style="width:120px;">
                                            <div class="btn-group btn-group-xs">
                                                <a href="#" class="btn btn-default switch-link" data-id="{{ link.id }}"><span
                                                        class="fa fa-fw fa-arrows-h"></span></a>
                                                <a href="{{ route('transactions.link.delete', [link.id]) }}"
                                                   class="btn btn-danger"><span class="fa fa-trash"></span></a>
                                            </div>
                                        </td>
                                        <td>
                                            {% if link.editable %}
                                                {{ link.link }}
                                            {% else %}
                                                {{ trans('firefly.'~link.link) }}
                                            {% endif %}
                                            "<a href="{{ route('transactions.show', link.group) }}"
                                                title="{{ link.description }}">{{ link.description }}</a>"

                                            ({{ link.amount|raw }})
                                            {% if '' != link.foreign_amount %}
                                                ({{ link.foreign_amount|raw }})
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}

                            </table>
                        </div>
                    </div>
                {% endif %}

                <!-- Attachments -->
                {% if attachments[journal.transaction_journal_id]|length > 0 %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'attachments'|_ }}</h3>
                        </div>
                        <div class="box-body no-padding">
                            {% include 'list.attachments' with {attachments: attachments[journal.transaction_journal_id]} %}
                        </div>
                    </div>
                {% endif %}

                <!-- Piggy bank events -->
                {% if events[journal.transaction_journal_id]|length > 0 %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'piggy_events'|_ }}</h3>
                        </div>
                        <div class="box-body no-padding">
                            <table class="table table-hover">
                                {% for event in events[journal.transaction_journal_id] %}
                                    <tr>
                                        <td style="width:30%;">{{ event.amount|raw }}</td>
                                        <td>
                                            <a href="{{ route('piggy-banks.show', [event.piggy_id]) }}">{{ event.piggy }}</a>
                                        </td>

                                    </tr>
                                {% endfor %}
                            </table>
                        </div>
                    </div>
                {% endif %}
                {% if logEntries[journal.transaction_journal_id]|length > 0 %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">
                                {{ 'audit_log_entries'|_ }}
                            </h3>
                        </div>
                        <div class="box-body no-padding">
                            {% include 'list.ale' with {logEntries: logEntries[journal.transaction_journal_id]} %}
                        </div>
                    </div>
                {% endif %}
            </div>
        {% endfor %}
    </div>
    {# modal for linking journals. Will be filled by AJAX #}
    <div class="modal fade" tabindex="-1" role="dialog" id="linkJournalModal">
    </div>
{% endblock %}
{% block scripts %}
    <script type="text/javascript" nonce="{{ JS_NONCE }}">
        var modalDialogURL = '{{ route('transactions.link.modal', ['%JOURNAL%']) }}';
        var acURL = '{{ route('api.v1.autocomplete.transactions-with-id') }}';
        var groupURL = '{{ route('transactions.show',['%GROUP%']) }}';
        var cloneGroupUrl = '{{ route('transactions.clone') }}';
        var cloneAndEditUrl = '{{ route('transactions.clone') }}?redirect=edit';


        $('.switch-link').on('click', switchLink);
        $('.reconcile-button').on('click', unreconcile);
        var switchLinkUrl = '{{ route('transactions.link.switch') }}';

        function unreconcile(e) {
            e.preventDefault();
            var obj = $(e.currentTarget);
            $.post(obj.attr('href'), {
                _token: token
            }).done(function () {
                location.reload();
            }).fail(function () {
                console.error('I failed :(');
            });

            return false
        }

        function switchLink(e) {
            e.preventDefault();
            var obj = $(e.currentTarget);
            $.post(switchLinkUrl, {
                _token: token,
                id: obj.data('id')
            }).done(function () {
                location.reload();
            }).fail(function () {
                console.error('I failed :(');
            });

            //alert(obj.data('id'));

            return false
        }
    </script>
    <script type="text/javascript" src="v1/js/lib/typeahead/typeahead.bundle.min.js?v={{ FF_VERSION }}"
            nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/transactions/show.js?v={{ FF_VERSION }}"
            nonce="{{ JS_NONCE }}"></script>
{% endblock %}

